import { getToken } from '@/utils/token'
import Vue from 'vue'
import VueRouter from 'vue-router'
// import Login from ''
// import Layout from '../views/Layout'
// import Home from ''
// import User from ''
// import Search from ''
// import from ''
// import  from ''
// import UserEdit from ''
// import Chat from ''

Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/layout/home' },
  {
    path: '/login',
    component: () =>
      import(/* webpackChunkName: "Login" */ '../views/login.vue')
  },
  {
    path: '/layout',
    component: () =>
      import(/* webpackChunkName: "Layout" */ '../views/Layout'),
    children: [{
      path: 'home',
      component: () =>
        import(/* webpackChunkName: "Home" */ '../views/Home')
    },
    {
      path: 'user',
      component: () =>
        import(/* webpackChunkName: "User" */ '../views/User')
    }]
  },
  {
    path: '/search',
    component: () =>
      import(/* webpackChunkName: "Search" */ '../views/Search/index')

  },
  {
    // 访问路径时传参
    path: '/results/:kw',
    component: () =>
      import(/* webpackChunkName: " SearchResults" */ '../views/Search/SearchResults.vue')
  },
  {
    path: '/article',
    component: () =>
      import(/* webpackChunkName: "ArticleDetail" */ '../views/ArticleDetail/index.vue')
  },
  {
    path: '/user_edit',
    component: () =>
      import(/* webpackChunkName: "UserEdit" */ '../views/User/userEdit')
  },
  {
    path: '/chat',
    component: () =>
      import(/* webpackChunkName: "Chat" */ '../views/chat')
  }

]

const router = new VueRouter({
  routes
})
// 导航守卫
// 登陆成功时，不允许切换路径进入登陆页面
router.beforeEach((to, from, next) => {
  if (getToken()?.length > 0 && to.path === '/login') {
    next(false)
  } else {
    next()
  }
})

export default router
